const util = require('../../utils/util.js');
const api = require('../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    size:8,
    allPage:0,
    count:0,
    diyList:[],
    loading:0,
    // 是否还有更多数据
    showMore: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let info = wx.getSystemInfoSync();
    let sysHeight = info.windowHeight - 100;
    this.setData({
        sysHeight: sysHeight,
        page: 1,
        size: 8,
        count:0,
        allPage:0,
        diyList:[],
        showMore:0,
        loading:0
    });
    this.getDiyList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    if (that.data.page >= that.data.allPage) {
        that.setData({
            showMore: 1
        });
        return false;
    }
    that.setData({
        page: that.data.page + 1
    });
    that.getDiyList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  startDiy(e){
    let res = util.loginNow();
    if (res == true) {
      let id=e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/diy/index?id='+id,
      })
    }
  },
  // 获取可定制产品列表
  getDiyList(){
    let that = this;
    util.requestApi(api.getCustom, {
        page: that.data.page,
        size: that.data.size
    }).then(function (res) {
        if (res.code == 200) {
          let list=res.content.data;
          that.setData({
            diyList:that.data.diyList.concat(list),
            count:res.content.count,
            showMore: list.length < that.data.size ? 1 : 0,
            loading:1,
            allPage:Math.ceil(res.content.count / that.data.size),
          });
        }
    })
  }
})